'use strict';

const conn = require('../../libs/Conn');

module.exports = async function (params) {
    /*
        质量监督：quality
        授权书:authorize
        承诺书:commitment
        建设单位:construction_employer
        设计单位:design
        施工单位:construction_contractor
        监理单位:supervisory
        勘察单位:survey
        图审单位:drawing
        */
    let sql = ``;
    /*质量监督*/
    if(params.quality_status ==='1' ){
        sql += `UPDATE quality_file SET file_no = '${params.quality_no}',submission_date = '${params.quality_subDate}',release_date = '${params.quality_releaseDate}',status = 1 WHERE engineering_id = '${params.engineering_id}';`
    }else{
        sql += `UPDATE quality_file SET file_no = '',submission_date = '',release_date = '',status = 0 WHERE engineering_id = '${params.engineering_id}';`
    }



    /*授权书*/
    /*建筑单位*/
    if(params.authorize_construction_employer_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.authorize_construction_employer_no}', user_id = '${params.authorize_construction_employer_user}',receipt_date = '${params.authorize_construction_employer_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 1;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 1;`
    }
    /*设计单位*/
    if(params.authorize_design_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.authorize_design_no}', user_id = '${params.authorize_design_user}',receipt_date = '${params.authorize_design_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 2;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 2;`
    }
    /*施工单位*/
    if(params.authorize_construction_contractor_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.authorize_construction_contractor_no}', user_id = '${params.authorize_construction_contractor_user}',receipt_date = '${params.authorize_construction_contractor_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 3;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 3;`
    }
    /*监理单位*/
    if(params.authorize_supervisory_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.authorize_supervisory_no}', user_id = '${params.authorize_supervisory_user}',receipt_date = '${params.authorize_supervisory_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 4;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 4;`
    }
    /*勘察单位*/
    if(params.authorize_survey_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.authorize_survey_no}', user_id = '${params.authorize_survey_user}',receipt_date = '${params.authorize_survey_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 5;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 5;`
    }
    /*图审单位*/
    if(params.authorize_drawing_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.authorize_drawing_no}', user_id = '${params.authorize_drawing_user}',receipt_date = '${params.authorize_drawing_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 6;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 1 AND unit_type = 6;`
    }



    /*承诺书*/
    /*建筑单位*/
    if(params.commitment_construction_employer_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.commitment_construction_employer_no}', user_id = '${params.commitment_construction_employer_user}',receipt_date = '${params.commitment_construction_employer_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 1;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 1;`
    }
    /*设计单位*/
    if(params.commitment_design_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.commitment_design_no}', user_id = '${params.commitment_design_user}',receipt_date = '${params.commitment_design_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 2;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 2;`
    }
    /*施工单位*/
    if(params.commitment_construction_contractor_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.commitment_construction_contractor_no}', user_id = '${params.commitment_construction_contractor_user}',receipt_date = '${params.commitment_construction_contractor_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 3;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 3;`
    }
    /*监理单位*/
    if(params.commitment_supervisory_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.commitment_supervisory_no}', user_id = '${params.commitment_supervisory_user}',receipt_date = '${params.commitment_supervisory_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 4;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 4;`
    }
    /*勘察单位*/
    if(params.commitment_survey_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.commitment_survey_no}', user_id = '${params.commitment_survey_user}',receipt_date = '${params.commitment_survey_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 5;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 5;`
    }
    /*图审单位*/
    if(params.commitment_drawing_status ==='1' ){
        sql += `UPDATE certificate_file SET certificate_no = '${params.commitment_drawing_no}', user_id = '${params.commitment_drawing_user}',receipt_date = '${params.commitment_drawing_date}',status = 1 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 6;`
    }else{
        sql += `UPDATE certificate_file SET certificate_no = '', user_id = '',receipt_date = '',status = 0 WHERE engineering_id ='${params.engineering_id}' AND certificate_type = 2 AND unit_type = 6;`
    }
    await conn(sql);
    return {success: true};
};